package com.xiaoyu.merchant.ui.fragment.main;


import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.techfansy.bottomDialog.BottomDialog;
import com.techfansy.moreImageSelected.ImagePicker;
import com.techfansy.moreImageSelected.MoreImageMethod;
import com.techfansy.moreImageSelected.bean.ImageItem;
import com.techfansy.moreImageSelected.ui.ImageGridActivity;
import com.xiaoyu.commonlib.network.CommonHttpResponser;
import com.xiaoyu.commonlib.utils.PreferencesUtil;
import com.xiaoyu.commonlib.utils.ToastUtil;
import com.xiaoyu.commonlib.utils.UIUtils;
import com.xiaoyu.merchant.R;
import com.xiaoyu.merchant.loader.StoreCreBanModel;
import com.xiaoyu.merchant.loader.StoreImageLoader;
import com.xiaoyu.merchant.model.GoodsCateGoryBean.GoodsCateGoryParam;
import com.xiaoyu.merchant.network.NetworkManager;
import com.xiaoyu.merchant.ui.dialog.AddSpecificationDialog;
import com.xiaoyu.merchant.ui.dialog.ChooseCateGoryDialog;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerListener;

import org.json.JSONArray;

import java.io.Serializable;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * @author jx
 * @date 创建时间 2018/12/28
 * @Description 上传商品
 */
public class UploadFragment extends Fragment {
//    private final int IMAGE_PICKER = 100;
//    @BindView(R.id.fragment_upload_detail_banner)
//    Banner mBanner;//轮播图
//    @BindView(R.id.fragment_upload_banner_img)
//    ImageView mBannerImg;
//    @BindView(R.id.fragment_upload_choose_classify_txt)
//    TextView classifyTxt;//选择的商品类别
//    @BindView(R.id.goods_show_img_layout)
//    LinearLayout showImgLayout;//商品展示图片布局
//    @BindView(R.id.goods_add_show_img)
//    ImageView addImg;//商品展示图片图片添加按钮
//
//    private View mView;
//    private MoreImageMethod moreImageMethod;
//    private ImagePicker imagePicker;
//    private List<ImageItem> mList = new ArrayList<>();//选中的图片list
//    private List<StoreCreBanModel> bannerImgs = new ArrayList<>();//轮播图数据列表
//    private List<ImageItem> mGoodsImgList = new ArrayList<>();//商品展示图列表
//
//    private GoodsCateGoryParam mSelectCatagory = null;//选中的商品类别
//    private ChooseCateGoryDialog mCateGoryDialog;//选择商品列表弹窗
//
//    private boolean isBanner = true;//true:轮播图  false:商品展示图
//
//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container,
//                             Bundle savedInstanceState) {
//        if (mView == null) {
//            mView = inflater.inflate(R.layout.fragment_upload, container, false);
//            //绑定fragment
//            ButterKnife.bind(this, mView);
//            initBanner();
//            moreImageMethod = new MoreImageMethod();
//            imagePicker = ImagePicker.getInstance();
//            imagePicker.clear();
//        }
//        ViewGroup parent = (ViewGroup) mView.getParent();
//        if (parent != null) {
//            parent.removeView(mView);
//        }
//        return mView;
//    }
//
//    /**
//     * 选择规格
//     */
//    @OnClick(R.id.fragment_upload_choose_specifications)
//    public void chooseSpecificationClick() {
//        AddSpecificationDialog dialog = new AddSpecificationDialog(getActivity(), true, true);
//        dialog.show();
//    }
//
//    /**
//     * 选择分类
//     */
//    @OnClick(R.id.fragment_upload_choose_classify)
//    public void chooseCategoryClick() {
//        if (mCateGoryDialog == null) {
//            mCateGoryDialog = new ChooseCateGoryDialog(getActivity());
//        }
//        mCateGoryDialog.setOnSelectCategoryListener(onSelectCategoryListener);
//        mCateGoryDialog.show(mSelectCatagory);
//
//    }
//
//    /**
//     * 轮播图显示
//     */
//    @OnClick(R.id.fragment_upload_banner_img)
//    public void takePhotoClick() {
////        moreImageMethod.setClick(this.getActivity(), mList, imagePicker);
//        isBanner = true;
//        photo();
//    }
//
//
//    /**
//     * 商品展示图图片添加按钮
//     */
//    @OnClick(R.id.goods_add_show_img)
//    public void addImgClick(){
//        isBanner = false;
//        photo();
//    }
//
//    /**
//     * 商品类别选择监听
//     */
//    private ChooseCateGoryDialog.OnSelectCategoryListener onSelectCategoryListener = new ChooseCateGoryDialog.OnSelectCategoryListener() {
//        @Override
//        public void onSelected(GoodsCateGoryParam category) {
//            if (category != null) {
//                mSelectCatagory = category;
//                classifyTxt.setText(mSelectCatagory.getGoodstagname());
//                classifyTxt.setTextColor(Color.BLACK);
//            }
//        }
//    };
//
//    private void photo() {
//        new BottomDialog(getActivity()).addItemSheet(com.example.alltoollib.R.string.photograph, new BottomDialog.SheetListener() {
//            @Override
//            public void setOnSheet(int i) {
//                PackageManager pm = getActivity().getPackageManager();
//                boolean permission = (PackageManager.PERMISSION_GRANTED ==
//                        pm.checkPermission("android.permission.RECORD_AUDIO", "packageName"));
//                if (permission) {
//                    //进入拍照
//                    Toast.makeText(getActivity(), "权限被禁止，无法打开相机", Toast.LENGTH_SHORT).show();
//                } else {
//                    imagePicker.takePicture(getActivity(), ImagePicker.REQUEST_CODE_TAKE);
//                }
//            }
//        }).addItemSheet(com.example.alltoollib.R.string.select_in_photo_album, new BottomDialog.SheetListener() {
//            @Override
//            public void setOnSheet(int i) {
//                Intent intent = new Intent(getActivity(), ImageGridActivity.class);
////                intent.putExtra(ImagePreviewActivity.ISORIGIN, isOrigin1);
//                intent.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (Serializable) mList);
//                startActivityForResult(intent, IMAGE_PICKER);
//            }
//        }).show();
//    }
//
//
//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        //接收回调的图片
//        if (isBanner){//轮播图
//            moreImageMethod.onActivityResult(requestCode, resultCode, data, imagePicker, mList, getActivity());
//            bannerImgs.clear();
//            Log.i("jx", "onActivityResult: 接收回调的图片===" + bannerImgs.size() + "---------" + mList.size());
//            for (int i = 0; i < mList.size(); i++) {
//                bannerImgs.add(i, new StoreCreBanModel(mList.get(i).getPaths(), 0));
//            }
//            if (mList.size() < 3) {
//                bannerImgs.add(mList.size(), new StoreCreBanModel(R.mipmap.banner, 1));
//            }
//            mBanner.setImages(bannerImgs);
//            mBanner.start();
//            mBannerImg.setVisibility(View.GONE);
//            mBanner.setVisibility(View.VISIBLE);
//        }else {//商品展示图
//            moreImageMethod.onActivityResult(requestCode, resultCode, data, imagePicker, mList, getActivity());
//            mGoodsImgList.clear();
//            showImgLayout.removeAllViews();
//            mGoodsImgList.addAll(mList);
//            if (mGoodsImgList.size() == 8){
//                addImg.setVisibility(View.GONE);
//            }
//            for (int i = 0; i < mGoodsImgList.size(); i++) {
//                ImageView view = new ImageView(getActivity());
//                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, UIUtils.dip2px(340));
//                view.setLayoutParams(params);
//                view.setScaleType(ImageView.ScaleType.FIT_XY);
//                Glide.with(getActivity()).load(mGoodsImgList.get(i).getPaths()).into(view);
//                showImgLayout.addView(view, i);
//            }
//        }
//
//    }
//
//    //初始化轮播图
//    private void initBanner() {
//        mBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
//        mBanner.setIndicatorGravity(BannerConfig.CENTER);
//        //设置是否自动轮播（不设置则默认自动）
//        mBanner.isAutoPlay(true);
//        //设置是否允许手动滑动轮播图
//        mBanner.setViewPagerIsScroll(true);
//        //设置轮播图片间隔时间（不设置默认为2000）
//        mBanner.setDelayTime(2000);
//        mBanner.setImageLoader(new StoreImageLoader());
//        mBanner.setOnBannerListener(new OnBannerListener() {
//            @Override
//            public void OnBannerClick(int position) {
//                if (bannerImgs.get(position).getType() == 1) {
//                    moreImageMethod.setClick(getActivity(), mList, imagePicker);
//                }
//            }
//        });
//    }
//
//
//    //预览之后按钮变成发布
//    @OnClick(R.id.fragment_upload_issue)
//    public void issueClick() {
//
//        String goodsname = "商品一号";
//        String goodssxqimg = "http://shangshangzhuangshi.oss-cn-beijing.aliyuncs.com//1538016224301.png,http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg";
//        String goodslbimg = "http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg,http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg";
//        String goodstagid = "8";
//        String goodsfreight = "25";
//        String goodsimg = "http://shangshangzhuangshi.oss-cn-beijing.aliyuncs.com//1538016224301.png";
//        String goodscontent = "商品的文字描述";
//        String detalis = "http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg^100cm^26^120,http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg^500cm^35^260";
//        String goodsdetailsname = "商品规格名字";
//        String goodsprice1 = "0.58";
//        String goodsinventory = "100";
//        String goodsdeimg = "http://yikabangb.oss-cn-beijing.aliyuncs.com//mmexport1548683750072.jpg";
//
//
//        NetworkManager.uploadShop(PreferencesUtil.getString(PreferencesUtil.TOKEN, ""), goodsname, goodssxqimg,
//                goodslbimg, goodstagid, goodsfreight, goodsimg, goodscontent, detalis, goodsdetailsname, goodsprice1,
//                goodsinventory, goodsdeimg, new CommonHttpResponser() {
//                    @Override
//                    public void onSuccess(String result) {
//                        ToastUtil.showToast(getActivity(), "发布成功");
//                        Log.i("jx", "onSuccess:是否成功== " + result);
//                    }
//
//                    @Override
//                    public void onFailed(String errorCode, String errorDiscription) {
//                        Log.i("jx", "onFailed:----- " + errorCode + errorDiscription);
//                    }
//                });
//    }


}
